﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 药品信息管理
{
    public partial class 入库单编辑 : Form
    {
        public static int rkdh = 0;//入库单号
        public bool is_new = true;//是否为新单的标志
        public int r0_flag = 0;//入库单flag
        public int AUTORKID = 0;
        public string cmdtext3 = "  BEGIN TRANSACTION  ";
        public DataTable dt = new DataTable();
        public 入库单编辑(int dh,int flag)
        {
            rkdh = dh;
            r0_flag = flag;
            InitializeComponent();
            first_show();
            
        }

        private void button7_Click(object sender, EventArgs e)//关闭
        {
            this.Close();
        }

        public void first_show()
        {
            int flag = 0;
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                string cmdtext6 = "select flag from yk_rkd0 where RKDH = " + rkdh;
                SqlCommand com = new SqlCommand(cmdtext6, cn);
                SqlDataReader read = com.ExecuteReader();
                if (read.Read())
                {
                    flag = (int)read[0];
                }
                read.Close();
                string cmdtext1 = "SELECT R1.ypbm 编码,YP.MC AS 名称  " +
                                " FROM YK_RKD1 R1     " +
                                "LEFT JOIN ZD_YP YP ON R1.YPBM = YP.BM   "+
                                " WHERE R1.RKDH = " + rkdh;
                SqlDataAdapter da=new SqlDataAdapter(cmdtext1,cn);
                
                da.Fill(dt);
                dataGridView1.DataSource = dt;
                
                if (dt.Rows.Count > 0)
                    is_new = false;
                
            }
            if (!is_new)
            {
                if (flag != 16)
                {
                    this.dataGridView1.AllowUserToAddRows = false;
                    textBox1.ReadOnly = true;
                    textBox1.Enabled = false;
                }
            }
            else

                this.dataGridView1.AllowUserToAddRows = true;
                
            
            
        }

        public void selectRow(string ypbm)
        {
            
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                string cmdtext2 = "SELECT R1.ypbm as 编码,YP.MC AS 名称,YP.GG as 规格,R1.SL as  数量," +
                                "R1.jj as 进价,R1.jjje as 进价金额,YPKC as 库存,PH as 批号,PZWH as 批准文号,  " +
                                "(select  convert(datetime,FPRQ-2)) as 发票日期,YP.BZDW as 入库单位, " +
                                "   R1.pj as 批价,R1.pjje as  批价金额,R1.lsj as  零价,R1.ljje as  零价金额,  " +
                                "R1.ph as  批号,R1.fph as  发票号,R1.bz as  备注, " +
                                "  (select  convert(datetime,R1.YXRQ-2))  as 有效期,YP.BZDJ as 执行单价,R1.SCCS as 生产厂商 ,R1.AUTORKID as AUTORKID " +
                                " FROM YK_RKD1 R1     " +
                                "LEFT JOIN ZD_YP YP ON R1.YPBM = YP.BM    " +
                                "   WHERE R1.RKDH =" + rkdh+" and R1.ypbm='"+ypbm+"' ORDER BY R1.AUTORKID ";
                SqlCommand com = new SqlCommand(cmdtext2, cn);
                SqlDataReader dr = com.ExecuteReader();
                if (dr.Read())
                {
                    textBox1.Text = dr["编码"].ToString();
                    textBox2.Text = dr["名称"].ToString();
                    textBox3.Text = dr["规格"].ToString();
                    textBox4.Text = dr["批准文号"].ToString();
                    //textBox5.Text = dr["生产厂商"].ToString();

                    textBox7.Text = dr["批号"].ToString();
                    dateTimePicker1.Text = dr["有效期"].ToString();

                    textBox10.Text = dr["发票号"].ToString();
                    textBox11.Text = dr["库存"].ToString();
                    dateTimePicker2.Text = dr["发票日期"].ToString();
                    textBox13.Text = dr["备注"].ToString();
                    textBox14.Text = dr["入库单位"].ToString();
                    textBox15.Text = dr["数量"].ToString();
                    textBox16.Text = dr["进价"].ToString();
                    textBox19.Text = dr["批价"].ToString();
                    textBox20.Text = dr["零价"].ToString();
                    textBox21.Text = dr["执行单价"].ToString();
                    textBox22.Text = dr["进价金额"].ToString();
                    textBox23.Text = dr["批价金额"].ToString();
                    textBox24.Text = dr["零价金额"].ToString();
                    textBox25.Text = ((decimal)dr["零价金额"] - (decimal)dr["进价金额"]).ToString();
                    AUTORKID = (int)dr["AUTORKID"];
                }
                else
                {
                    textBox1.Text = "";
                    textBox2.Text = "";
                    textBox3.Text = "";
                    textBox4.Text = "";
                    //textBox5.Text = dr["生产厂商"].ToString();

                    textBox7.Text = "";
                    dateTimePicker1.Text = "";

                    textBox10.Text = "";
                    textBox11.Text = "";
                    dateTimePicker2.Text = "";
                    textBox13.Text = "";
                    textBox14.Text = "";
                    textBox15.Text = "";
                    textBox16.Text = "";
                    textBox19.Text = "";
                    textBox20.Text = "";
                    textBox21.Text = "";
                    textBox22.Text = "";
                    textBox23.Text = "";
                    textBox24.Text = "";
                    textBox25.Text = "";
                }
            }

            if (r0_flag == 19)
            {
                button1.Enabled = false;
                button4.Enabled = false;
                button5.Enabled = false;
                button6.Enabled = false;

            }
            else if (r0_flag == 17)
            {
                button1.Enabled = false;
                button4.Enabled = false;
                button5.Enabled = false;
                button6.Enabled = false;


            }
            else if (r0_flag == 16)
            {
                button1.Enabled = true;
                button4.Enabled = true;
                button5.Enabled = true;
                button6.Enabled = true;

            }
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            update_currentRow();
            selectRow(dataGridView1.CurrentRow.Cells["编码"].Value.ToString());
        }

        private void 入库单编辑_Load(object sender, EventArgs e)
        {
            dataGridView2.Visible = false;
            dataGridView3.Visible = false;
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.MultiSelect = false;
            this.dataGridView2.AllowUserToAddRows = false;
            this.dataGridView2.MultiSelect = false;
            this.dataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView2.ReadOnly = true;
            this.dataGridView3.AllowUserToAddRows = false;
            this.dataGridView3.MultiSelect = false;
            this.dataGridView3.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView3.ReadOnly = true;
            this.dataGridView1.ReadOnly = true;
            if (r0_flag != 0)
            {
                dataGridView1.CurrentRow.Selected = true;
                selectRow(dataGridView1.CurrentRow.Cells["编码"].Value.ToString());
            }

        }

        private void button5_Click(object sender, EventArgs e)//删除
        {
            string ypbm= dataGridView1.CurrentRow.Cells["编码"].Value.ToString();
            cmdtext3 = cmdtext3 + "   DELETE from YK_RKD1 WHERE RKDH = " + rkdh + "  AND YPBM = '" + ypbm + "'  AND AUTORKID = " + AUTORKID;
            dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]);
            dataGridView1.Rows[0].Selected = true;
            selectRow(dataGridView1[0, 0].Value.ToString());
        }

        private void button6_Click(object sender, EventArgs e)//取消
        {
            this.Close();
        }

        private void button4_Click(object sender, EventArgs e)//新增
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请输入药品");
                return;
            }
            string dtp1 = dateTimePicker1.Value.ToShortDateString();
            string dtp2 = dateTimePicker2.Value.ToShortDateString();
            int dpt_1 = 0;
            int dpt_2 = 0;
            string cmdtext6 = "select  convert(int,(select Convert(DateTime,'" + dtp1 + "')))+2 ,convert(int,(select Convert(DateTime,'" + dtp2 + "')))+2";
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                SqlCommand com=new SqlCommand(cmdtext6,cn);
                SqlDataReader read = com.ExecuteReader();
                if (read.Read())
                {
                    dpt_1 = (int)read[0];
                    dpt_2 = (int)read[1];
                }
                read.Close();
                cn.Close();
            }
            cmdtext3 = cmdtext3 + "    insert into yk_rkd1(rkdh,ypbm,yxrq,ph,jj,lsj,cklsj,sl,sysl,je,jjje,ljje,fph,fprq,yfje,pzwh,pj,pjje)    " +
                                  "   values (" + rkdh + ",'" + textBox1.Text.ToString() + "'," +
                                  dpt_1 + "," +
                                  "'" + textBox7.Text.ToString() + "'," +
                                  textBox16.Text + "," +
                                   textBox20.Text + "," +
                                   textBox21.Text + "," +
                                   textBox15.Text + "," +
                                   textBox15.Text + "," +
                                    textBox24.Text + "," +
                                    textBox22.Text + "," +
                                     textBox24.Text + ",'" +
                                     textBox10.Text.ToString() + "'," +
                                    dpt_2 + " ," +
                                    textBox22.Text + ",'" +
                                    textBox4.Text.ToString() + "',0,0)    ";



            cmdtext3 = cmdtext3 + "    update yk_rkd1  set bzsl=yp.bz,ypkc=" + textBox11.Text + "  from zd_yp yp  where rkdh=" + rkdh + "  and yp.bm=ypbm  and ypbm='" + textBox1.Text.ToString() + "'";
            DataRow dr = dt.NewRow();
            dr[0] = textBox1.Text.ToString();
            dr[1] = textBox2.Text.ToString();
            dt.Rows.Add(dr);
            dataGridView1.DataSource = dt;



        }

        private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            select_bm();
        }

        public void select_bm()//下拉框
        {
            string cmdtext5 = "SELECT bm as 编码,MC AS 名称,GG as 规格,bzdj as 执行单价,bzdw as 入库单位  from zd_yp where  dh = '" + dataGridView2.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' or bm = '" + dataGridView2.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' or mc = '" + dataGridView2.CurrentRow.Cells["编码"].Value.ToString().Trim() + "' ";
            SqlDataReader read2;
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                SqlCommand com = new SqlCommand(cmdtext5, cn);
                SqlDataReader read1 = com.ExecuteReader();
                if (read1.Read())
                {
                    textBox1.Text = read1["编码"].ToString();
                    textBox2.Text = read1["名称"].ToString();
                    textBox3.Text = read1["规格"].ToString();
                    textBox21.Text = read1["执行单价"].ToString();
                    textBox20.Text = read1["执行单价"].ToString();
                    textBox14.Text = read1["入库单位"].ToString();
                }
                read1.Close();
            }
            //药品库存
            string cmdtext6 = "select SUM(SL) AS KCSL from YP_CRKJZ   where YPBM='"+textBox1.Text.ToString()+"'";

            SqlConnection cn1 = new SqlConnection(SQL.conStr);
            {
                cn1.Open();
                SqlCommand com = new SqlCommand(cmdtext6, cn1);
                read2 = com.ExecuteReader();
            }


            if (read2.Read())
            {
                textBox11.Text = read2[0].ToString();
                if (textBox11.Text == "")
                    textBox11.Text = "0";
            }
            else
                textBox11.Text = "0";

   
            dataGridView2.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)//确定
        {
            cmdtext3 = cmdtext3 + "   COMMIT TRANSACTION ";
            try
            {
                SqlConnection cn = new SqlConnection(SQL.conStr);
                {
                    cn.Open();
                    SqlCommand com = new SqlCommand(cmdtext3, cn);
                    com.ExecuteNonQuery();
                }
            }
            catch (Exception ee)
            {
                cmdtext3 = "rollback";
                SqlConnection cn = new SqlConnection(SQL.conStr);
                {
                    cn.Open();
                    SqlCommand com = new SqlCommand(cmdtext3, cn);
                    com.ExecuteNonQuery();
                }
                MessageBox.Show("ERROR!" + ee.ToString());
            }
            this.Close();
        }

        public void update_currentRow()//更改
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请输入药品");
                return;
            }
            string dtp1 = dateTimePicker1.Value.ToShortDateString();
            string dtp2 = dateTimePicker2.Value.ToShortDateString();
            cmdtext3 = cmdtext3 + "   UPDATE YK_RKD1 SET   " +
                                      "RKDH ="+rkdh+"," +
                                     " YPBM = '"+textBox1.Text.ToString()+"'," +
                                     //" BZSL = 60,"+
                                      //"SCRQ = 0,"+
                                      "YXRQ = (select  convert(int,(select Convert(DateTime,'" + dtp1 + "')))+2 ) ," +
                                      //" SCCS = '51',"+
                                     " PH = '" + textBox7.Text.ToString() + "'," +
                                     " RLL = 0," +
                                     " GYL = 0," +
                                     " JJ = " + textBox16.Text + "," +
                                     //"PJ = 0,"+
                                     " LSJ = " + textBox20.Text + "," +
                                     " CKLSJ = " + textBox21.Text + "," +
                                     " SL = " + textBox15.Text + "," +
                                    "  JE = " + textBox24.Text + "," +
                                    "  JJJE = " + textBox22.Text + "," +
                                    //"  PJJE = 0," +
                                     " LJJE = " + textBox24.Text + "," +
                                     " FPH = '" + textBox10.Text.ToString() + "'," +
                                    "  FPRQ = (select  convert(int,(select Convert(DateTime,'" + dtp2 + "')))+2 ) ," +
                                     " YFJE = " + textBox22.Text + "," +
                                     " FKRQ = 0," +
                                     " SYSL = " + textBox15.Text + "," +
                                     " HWBM = ''," +

                                     " FLAG = 0," +
                                     " ZCSB = ''," +
                                     " PZWH = '" + textBox4.Text.ToString() + "'," +
                                     " GYSBM = '14'," +
                                     " LYRKID = 0," +
                                     " ZYCD = ''," +
                                    "  ZYDJ = ''," +
                                    "  WKZL = ''," +
                                    "  BZZL = ''," +
                                     " YSJL = ''," +
                                     " HAVEBQ = ''," +
                                    "  HAVEBS = ''," +
                                    "  HAVESMS = ''," +
                                    "  BZ = ''," +
                                    "  YSRY = ''," +
                                    "  YSRQ = 0," +
                                    "  BGY = ''," +
                                     " YPKC = "+textBox11.Text+"," +
                                    "  SCXKZ = ''," +
                                    "  WSXKZ = ''," +
                                    "  CKCBDJ = 0," +
                                     " CKPJ = 0" +
                                    "   WHERE RKDH = " + rkdh + " AND YPBM = '" + textBox1.Text.ToString() + "' AND AUTORKID = " + AUTORKID;
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)//药品编码press
        {
            string cmdtext4 = "select bm 编码,MC AS 名称 from zd_yp where dh like '" + textBox1.Text.Trim() + "%' or bm like '" + textBox1.Text.Trim() + "%' or mc like '" + textBox1.Text.Trim() + "%'";
            SqlConnection cn = new SqlConnection(SQL.conStr);
            {
                cn.Open();
                SqlDataAdapter da1 = new SqlDataAdapter(cmdtext4, cn);
                DataTable dt = new DataTable();
                da1.Fill(dt);
                dataGridView2.DataSource = dt;
                dataGridView2.Visible = true;
            }



        }

        private void textBox15_KeyPress(object sender, KeyPressEventArgs e)//入库数量press
        {
            try
            {
                if (e.KeyChar == '\r')
                {
                    Decimal ljje = System.Convert.ToDecimal(textBox20.Text) * System.Convert.ToDecimal(textBox15.Text);
                    textBox24.Text = ljje.ToString();
                    textBox16.Focus();
                }
            }
            catch (Exception ee) { MessageBox.Show(ee.ToString()); }
        }

        private void textBox16_KeyPress(object sender, KeyPressEventArgs e)//进价press
        {
            try
            {
                if (e.KeyChar == '\r')
                {
                    Decimal jjje = System.Convert.ToDecimal(textBox16.Text) * System.Convert.ToDecimal(textBox15.Text);
                    textBox22.Text = jjje.ToString();
                    Decimal jlj = System.Convert.ToDecimal(textBox24.Text) - System.Convert.ToDecimal(textBox22.Text);
                    textBox25.Text = jlj.ToString();
                }
            }
            catch (Exception ee) { MessageBox.Show(ee.ToString()); }
        }

        private void dataGridView3_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)//批准文号datagridview
        {
            textBox4.Text = dataGridView3.CurrentCell.Value.ToString();
            dataGridView3.Visible = false;
        }

        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)//批准文号press
        {
            if (e.KeyChar == '\r')
            {
                string cmdtext4 = "select PZWH as 批准文号 from YK_ZDPZWH where ypbm='" + textBox1.Text.ToString() + "'";
                SqlConnection cn = new SqlConnection(SQL.conStr);
                {
                    cn.Open();
                    SqlDataAdapter da1 = new SqlDataAdapter(cmdtext4, cn);
                    DataTable dt = new DataTable();
                    da1.Fill(dt);
                    dataGridView3.DataSource = dt;
                    dataGridView3.Visible = true;
                }
            }
            else
                dataGridView3.Visible = false;
        }
    }
}